package cn.sinata.wanglin.mine.activity;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class PrivateSettingActivity extends TitleActivity {

    @BindView(R.id.tv_needauth)
    TextView tv_needauth ;//需要验证
    @BindView(R.id.tv_cannotfindme)
    TextView tv_cannotfindme ;//找不到我
    @Override
    protected int setContentId() {
        return R.layout.activity_private_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("隐私");
        boolean selectedNeedAuth= SharedPreferencesUtils.getBoolean(Constant.SP_NEED_AUTH,true);
        boolean selectedFindMe= SharedPreferencesUtils.getBoolean(Constant.SP_FIND_ME,false);
        tv_needauth.setSelected(selectedNeedAuth);
        tv_cannotfindme.setSelected(selectedFindMe);
    }

    @OnClick({R.id.ttlv_shieldperson,R.id.ttlv_blacklist,R.id.tv_cannotfindme,R.id.tv_needauth})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ttlv_shieldperson:
                //描述:屏蔽的人
                ActivityUtil.create(mContext).go(ShieldpersonActivity.class)
                        .start();
                break;
            case R.id.ttlv_blacklist:
                //描述:黑名单
                ActivityUtil.create(mContext).go(BlacklistActivity.class)
                        .start();
                break;
            case R.id.tv_needauth:
                //描述:验证
                showDialog("修改中...");
                needAuth();

                break;
            case R.id.tv_cannotfindme:
                //描述:找不到我
                showDialog("修改中...");
                canNotFindMe();
                break;
        }
    }

    private void needAuth() {
        final boolean selected= tv_needauth.isSelected();
        String msg="";
        if(!selected){
             msg="0";
        }else{
             msg="1";
        }
        String userid=SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.updUser(userid,msg,Constant.ALT_valid).subscribe(new ResultDataSubscriber<JsonObject>(this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                tv_needauth.setSelected(!selected);
                SharedPreferencesUtils.save(Constant.SP_NEED_AUTH,!selected);

            }
        });
    }
    private void canNotFindMe() {
        final boolean selected= tv_cannotfindme.isSelected();
        String msg="";
        if(!selected){
            msg="0";
        }else{
            msg="1";
        }
        String userid=SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.updUser(userid,msg,Constant.ALT_isforeign).subscribe(new ResultDataSubscriber<JsonObject>(this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                tv_cannotfindme.setSelected(!selected);
                SharedPreferencesUtils.save(Constant.SP_FIND_ME,!selected);
            }
        });
    }
}
